home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / ensvis.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  9KB  |  287 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "ensvis.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_EnsureVisible()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107.  
  108. #define IMAGESIZE 16
  109.  
  110. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HWND hTree = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE  :
  119.               {
  120.                  HIMAGELIST hImage;
  121.  
  122.                  InitCommonControls();
  123.  
  124.                  hImage   = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  125.                                               ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  126.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  127.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  128.                                          WS_VSCROLL | TVS_HASLINES | 
  129.                                          TVS_HASBUTTONS | TVS_LINESATROOT |
  130.                                          TVS_DISABLEDRAGDROP,
  131.                                          10, 100, 100, 100,
  132.                                          hWnd,
  133.                                          (HMENU)1,
  134.                                          hInst,
  135.                                          NULL);
  136.  
  137.                  if ( hTree )
  138.                  {
  139.                     int i;
  140.  
  141.                     // Associate the image list with the tree view.
  142.                     //.............................................
  143.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  144.  
  145.                     // Add the images to the tree view image list.
  146.                     //............................................
  147.                     for ( i=0; i<4; i++ )
  148.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  149.  
  150.                     AddItemsToTree( hTree );
  151.                  }
  152.               }
  153.               break;
  154.  
  155.       case WM_SIZE :
  156.               if ( hTree )
  157.                  MoveWindow( hTree, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  158.               break;
  159.  
  160.       case WM_COMMAND :
  161.               switch( LOWORD( wParam ) )
  162.               {
  163.                  case IDM_VIEWLAST :
  164.                  case IDM_VIEWFIRST :
  165.                         {
  166.                            HTREEITEM hItem;
  167.  
  168.                            // Get the first item in the tree.
  169.                            //................................
  170.                            hItem = TreeView_GetRoot( hTree );
  171.  
  172.                            // Find the last item if we are viewing the last.
  173.                            //...............................................
  174.                            if ( hItem && LOWORD( wParam ) == IDM_VIEWLAST )
  175.                            {
  176.                               while ( hItem )
  177.                               {
  178.                                  if ( TreeView_GetNextSibling( hTree, hItem ) )
  179.                                     hItem = TreeView_GetNextSibling( hTree, hItem );
  180.                                  else if ( TreeView_GetChild( hTree, hItem ) )
  181.                                     hItem = TreeView_GetChild( hTree, hItem );
  182.                                  else
  183.                                     break;
  184.                               }
  185.                            }
  186.  
  187.                            // Select the appropriate item.
  188.                            //.............................
  189.                            if ( hItem )
  190.                            {
  191.                               TreeView_EnsureVisible( hTree, hItem );
  192.                               TreeView_SelectItem( hTree, hItem );
  193.                            }
  194.                         }
  195.                         break;
  196.  
  197.                  case IDM_ABOUT :
  198.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  199.                         break;
  200.  
  201.                  case IDM_EXIT :
  202.                         DestroyWindow( hWnd );
  203.                         break;
  204.               }
  205.               break;
  206.       
  207.       case WM_DESTROY :
  208.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  209.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  210.  
  211.               PostQuitMessage(0);
  212.               break;
  213.  
  214.       default :
  215.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  216.    }
  217.  
  218.    return( 0L );
  219. }
  220.  
  221.  
  222. VOID AddItemsToTree( HWND hTree )
  223. {
  224.    int             i, j;
  225.    char            szTmp[40];
  226.    TV_INSERTSTRUCT tv;
  227.    HTREEITEM       hParent;
  228.  
  229.    tv.hInsertAfter = TVI_LAST;
  230.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  231.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  232.  
  233.    // Add the items to the tree view.
  234.    //................................
  235.    for ( i=0; i<20; i++ )
  236.    {
  237.       wsprintf( szTmp, "%s %d", lpszData[0], i+1 );
  238.  
  239.       tv.hParent      = TVI_ROOT;
  240.       tv.item.pszText = szTmp;
  241.       tv.item.iImage  = 0;
  242.       tv.item.iSelectedImage = 0;
  243.       tv.item.lParam         = 0;
  244.  
  245.       hParent = TreeView_InsertItem( hTree, &tv );
  246.       tv.hParent = hParent;
  247.  
  248.       // Add child items.
  249.       //.................
  250.       for( j=1; j<4; j++ )
  251.       {
  252.          tv.item.pszText = (LPTSTR)lpszData[j];
  253.          tv.item.iImage  = j;
  254.          tv.item.iSelectedImage = j;
  255.          tv.item.lParam         = 1;
  256.  
  257.          TreeView_InsertItem( hTree, &tv );
  258.       }
  259.    }
  260. }
  261.  
  262.  
  263. LRESULT CALLBACK About( HWND hDlg,           
  264.                         UINT message,        
  265.                         WPARAM wParam,       
  266.                         LPARAM lParam)
  267. {
  268.    switch (message) 
  269.    {
  270.        case WM_INITDIALOG: 
  271.                return (TRUE);
  272.  
  273.        case WM_COMMAND:                              
  274.                if (   LOWORD(wParam) == IDOK         
  275.                    || LOWORD(wParam) == IDCANCEL)    
  276.                {
  277.                        EndDialog(hDlg, TRUE);        
  278.                        return (TRUE);
  279.                }
  280.                break;
  281.    }
  282.  
  283.    return (FALSE); 
  284. }
  285.  
  286.  
  287.